import { Link, Outlet, useLocation, useRoutes, history } from "umi";
import React, { useState, useEffect } from "react";
import { routes } from "@/router/index";
import SildeBar from "./components/sidebar";
import { MenuFoldOutlined, MenuUnfoldOutlined } from "@ant-design/icons";
import { Button, Layout, Menu, theme } from "antd";
import Login from "@/pages/login";
const { Header, Content } = Layout;
export default function LayoutMain() {
  const location = useLocation();
  // console.log("🚀 ~ LayoutMain ~ location:", location)
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();
  const onMsg = (e: MessageEvent) => {

    console.log("msg origin:", e.origin, "data:", e.data);
    const { type, path } = (e.data || {}) as { type?: string; path?: string };
    if (path && typeof path === "string") {
      history.push(path);
    }
  };
  // useEffect(() => {
  //   window.addEventListener("message", onMsg);
  //   // 可选：告诉父页已就绪，父页再发 navigate
  //   // window.parent?.postMessage({ type: 'ready' }, '*');

  //   return () => window.removeEventListener("message", onMsg);
  // }, []);
  return (
    <>
      <Layout style={{ height: "100vh" }}>
        {/* <SildeBar collapsed={collapsed}></SildeBar> */}
        <Layout>
          <Header style={{ padding: 0, background: colorBgContainer }}>
            <Button
              type="text"
              icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
              onClick={() => setCollapsed(!collapsed)}
              style={{
                fontSize: "16px",
                width: 64,
                height: 64,
              }}
            />
          </Header>
          <Content
            style={{
              margin: "24px 16px",
              padding: 24,
              minHeight: 280,
              background: colorBgContainer,
              borderRadius: borderRadiusLG,
            }}
          >
            <Outlet />
          </Content>
        </Layout>
      </Layout>
    </>
  );
}
